<?php
	/*
		PandaForum: PHP Forum Engine
		
		Jesse Gill, 0108377707
	*/
	
	require_once('../classes/DBConnection.php');
	require('../config.php');
	
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	
	$connection = new DBConnection();
	$connection->connect($host, $username, $password, $dbname);
	
	if (! $connection->is_connected())
	{
		die($connection->get_error());
	}
	
	$searchterm = 			$connection->clean_var($_GET['searchterm']);
	$page = 				$connection->clean_var(intval($_GET['page']));
	
	$result = $connection->get_result("SELECT * FROM user WHERE username LIKE '%$searchterm%'");
	
	if (! $result)
		die($connection->get_error());
	
	$numpages = ceil($result->num_rows / $usersperpage);
	
	$lowerlimit = ($page - 1) * $usersperpage;
	$upperlimit = $usersperpage;
	
	$result->free();
	
	$result = $connection->get_result("SELECT * FROM user WHERE username LIKE '%$searchterm%' LIMIT $lowerlimit, $upperlimit");
	
	if (! $result)
		die($connection->get_error());
?>
	<div class="member-2">
		<div class="membername">Name</div>
		<div class="memberposts"># Posts</div>
		<div class="membertopics"># Topics</div>
	</div>
<?php
	$i = 1;
	
	while ($obj = $result->fetch_object())
	{
		$posts = $connection->get_result("SELECT * FROM post WHERE posterid=$obj->id");
		$numposts = ($posts) ? $posts->num_rows : 0;
		$posts->free();
		
		$topics = $connection->get_result("SELECT * FROM topic WHERE creatorid=$obj->id");
		$numtopics = ($topics) ? $topics->num_rows : 0;
		$topics->free();
?>
		<div class="member-<?php echo $i; ?>">
			<div class="membername"><?php echo $obj->username; ?></div>
			<div class="memberposts"><?php echo $numposts; ?></div>
			<div class="membertopics"><?php echo $numtopics; ?></div>
		</div>
<?php
		if ($i == 1)
		{
			$i = 2;
		}
		else
		{
			$i = 1;
		}
	}
?>
	<div id="pagelist">
<?php
		for ($p = 0; $p < $numpages; $p++)
		{
?>
			<a href="javascript:members_users_refreshlist('<?php echo $searchterm; ?>', <?php echo ($p + 1); ?>)"><?php echo ($p + 1); ?>, </a>
<?php
		}
?>
	</div>
<?php
	$result->free();
	$connection->close();
?>